package cn.jinjimi.app.custom.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.controller.CustomerController;
import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.LocalEvent.CustomClueStaticUpdateEvent;
import cn.jinjimi.app.custom.entity.ClueStatistic;
import cn.jinjimi.app.custom.event.ClueStaticEvent;
import cn.jinjimi.app.custom.event.ClueUpdateEvent;
import cn.jinjimi.app.custom.event.CustomClueRefreshEvent;
import cn.jinjimi.app.custom.ui.fragment.CustomerClueListFragment;
import cn.jinjimi.app.main.adapter.EmptyAdapter;
import cn.jinjimi.app.main.adapter.FragmentPageAdapter;
import cn.jinjimi.app.main.extend.BaseFragment;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.biz.CustomClueHeadView;
import cn.jinjimi.app.main.widget.MyViewPager;
import cn.jinjimi.app.main.widget.PagerSlidingTabStrip;
import cn.jinjimi.app.main.widget.XListView.XListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Roy
 * Date: 16/12/30
 */
public class CustomClueActivity extends ToolBarCommonActivity
        implements ViewPager.OnPageChangeListener, XListView.IXListViewListener {

    private MyViewPager mViewPager;
    private PagerSlidingTabStrip indicator;
    private FragmentPageAdapter mAdapter;

    private XListView mListView;
    private CustomClueHeadView mHeadView;

    private boolean refreshingStatic, refreshingList;

    public static void launch(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, CustomClueActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_custom_clue_layout);
    }

    @Override
    protected void initial() {
        setTitle(getString(R.string.customer_promote));
        initView();
        initListener();
        initData();
    }

    private void initView() {
        mListView = (XListView) findViewById(R.id.listview);
        mListView.setPullLoadEnable(false);
        mListView.clearLoadAnimate(true);
        mListView.setStopTouchEvent(true);
        mHeadView = new CustomClueHeadView(this);
        mListView.setAdapter(new EmptyAdapter());
        mListView.addHeaderView(mHeadView);

        mViewPager = (MyViewPager) findViewById(R.id.view_pager);

        indicator = (PagerSlidingTabStrip) findViewById(R.id.indicator_tabs);
        indicator.setOnPageChangeListener(this);
    }

    private void initListener() {
        mListView.setXListViewListener(this);
    }

    private void initData() {

        List<CustomerClueListFragment> fragments = new ArrayList<>();
        fragments.add(CustomerClueListFragment.create(getString(R.string.clue_source_all), CustomerClueListFragment.SourceAll));
        fragments.add(CustomerClueListFragment.create(getString(R.string.clue_source_drp), CustomerClueListFragment.SourceDrp));
//        fragments.add(CustomerClueListFragment.create(getString(R.string.clue_source_redpack), CustomerClueListFragment.SourceRedPack));
        fragments.add(CustomerClueListFragment.create(getString(R.string.clue_source_other), CustomerClueListFragment.SourceOther));

        mAdapter = new FragmentPageAdapter(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mAdapter);
        indicator.setViewPager(mViewPager);

        CustomerController.getClueStatic();

        onPageSelected(0);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        analysePage(position, ((BaseFragment) mAdapter.getItem(position)).getTitle());
    }

    @EventTrace(EventID.APP_CUSTOMER_ZHANYE_TAB)
    private void analysePage(@EventTraceParam(value = EventDataField.ParamPostion, isValueAutoPlusOne = true) int position, @EventTraceParam(EventDataField.ParamId) String title) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onRefresh() {
        refreshingStatic = true;
        refreshingList = true;
        CustomerController.getClueStatic();
        ((CustomerClueListFragment) mAdapter.getItem(mViewPager.getCurrentItem())).onRefresh();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeadView != null) {
            mHeadView.onDestroy();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onClueStaticEvent(ClueStaticEvent event) {
        switch (event.state) {
            case DataEvent.SUCC:
                if (event.data != null && event.data.data != null) {
                    ClueStatistic clueStatistic = event.data.data;
                    mHeadView.setData(clueStatistic);
                } else {
                    ToastUtils.toastMessage(CustomClueActivity.this, getString(R.string.error_bad_network));
                }
                break;
            case DataEvent.FAIL:
                ToastUtils.toastMessage(CustomClueActivity.this, getString(R.string.error_bad_network));
                break;
        }
        EventBus.getDefault().post(new CustomClueRefreshEvent(CustomClueRefreshEvent.TYPE_STATIC));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomClueRefreshEvent(CustomClueRefreshEvent event) {
        int type = event.getType();
        if (type == CustomClueRefreshEvent.TYPE_STATIC) {
            refreshingStatic = false;
        } else if (type == CustomClueRefreshEvent.TYPE_LIST) {
            refreshingList = false;
        }
        if (!refreshingList && !refreshingStatic) {
            mListView.stopRefresh();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onClueUpdateEvent(ClueUpdateEvent ev) {
        if (mAdapter == null || mAdapter.getCount() == 0) return;
        for (int i = 0; i < mAdapter.getCount(); i++) {
            ((CustomerClueListFragment) (mAdapter.getItem(i))).onReceiveClueEvent(ev);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomClueStaticUpdateEvent(CustomClueStaticUpdateEvent event) {
        CustomerController.getClueStatic();
    }

}